from __future__ import division
import random
import math
import os
import sys
from sets import Set

print "Begins..."
print "updated 2014/12/09 by Wei"
keyValueDict = {}
ofn = "/home/vgc/wei/workspace/NYU_IRTK/polyIRToolkit_Wei/trainingFile_positiveExamplesONLY_20141209.arff"
ofh = open(ofn,"w")

print "construct Meta Data Lines"
'''
ofh.write("@relation learning_to_prune_20141209" + "\n")
ofh.write("@attribute resultRank numeric" + "\n")
ofh.write("@attribute qid numeric" + "\n")
ofh.write("@attribute queryTerm string" + "\n")
ofh.write("@attribute docid numeric" + "\n")
ofh.write("@attribute absRankInList numeric" + "\n")
ofh.write("@attribute listLength numeric" + "\n")
ofh.write("@attribute relRankInList numeric" + "\n")
ofh.write("@attribute impactScore numeric" + "\n")
ofh.write("@attribute TOP10Label {True,False}" + "\n")
ofh.write("@data" + "\n")
'''

print "construct Training Data Lines"
#####################################
ifna1 = "/home/vgc/wei/workspace/NYU_IRTK/polyIRToolkit_Wei/features_part1_negativeExamples_20141208"
ifna2 = "/home/vgc/wei/workspace/NYU_IRTK/polyIRToolkit_Wei/features_part2_negativeExamples_20141208"
ifnb1 = "/home/vgc/wei/workspace/NYU_IRTK/polyIRToolkit_Wei/features_part1_positiveExamples_20141208"
ifnb2 = "/home/vgc/wei/workspace/NYU_IRTK/polyIRToolkit_Wei/features_part2_positiveExamples_20141208"
counter = 0
fileTuplea = (ifna1,ifna2,"False")
fileTupleb = (ifnb1,ifnb2,"True")
fileTupleList = []
# fileTupleList.append(fileTuplea)
fileTupleList.append(fileTupleb)

for currentFileTuple in fileTupleList:  
    (ifn1,ifn2,label) = currentFileTuple
    print "ifn1:",ifn1
    print "ifn2:",ifn2
    ifh1 = open(ifn1,"r")
    ifh2 = open(ifn2,"r")
    l1 = ifh1.readline()
    lineCounter = 1
    while l1:
        le1 = l1.strip().split(" ")
        key = le1[0] + "," + le1[1] + "," + le1[2] + "," + le1[3]
        valueString = le1[4] + "," + le1[5] + "," + str(float(le1[4]) / float(le1[5]))
        if key not in keyValueDict:
            keyValueDict[key] = valueString
        else:
            print key
            exit(1)
        l1 = ifh1.readline()
        lineCounter += 1
        #if lineCounter == 1000:
        #    break
    print "len(keyValueDict):",len(keyValueDict)
    #####################################
    l2 = ifh2.readline()
    lineCounter = 1
    while l2:
        le2 = l2.strip().split(" ")
        key = le2[0] + "," + le2[1] + "," + le2[2] + "," + le2[3]
        if key in keyValueDict:
            outputLine = key + "," + keyValueDict[key] + "," + le2[4] + "," + label + "\n"
            # print outputLine,
            ofh.write(outputLine)
        else:
            print key,"missing"
            counter += 1
            # exit(1)
        l2 = ifh2.readline()
        lineCounter += 1
        
        #if lineCounter == 1000:
        #    break
    print "Overall:"
    print "ifn1:",ifn1
    print "ifn2:",ifn2
    print "ofn:",ofn
    print
    keyValueDict = {}
#####################################
print "counter:",counter
ifh1.close()
ifh2.close()
ofh.close()
print "END."